const app = getApp();
Component({
  data: {
    showAction: false,
    sortOptions: [
      { text: '时间', value: 'time' },
      { text: '按年', value: 'year' },
      { text: '按月', value: 'month' },
      { text: '名称', value: 'name' }
    ],
    id: 0,
    projectList: [
      {
        "id": 0,
        "name": "山语时光项目",
        "description": "此项目甚好，值得推广",
        "address": "山东省济南市历下区泉城路136号2栋402",
        "clientName": "徐灵云",
        "chargeName": "薛岳",
        "deviceCount": 8,
        "addTime": "2024-12-10"
      }
    ],
    actions: [
      { name: '查看', action: 'view' },
      { name: '编辑', action: 'edit' },
      { name: '删除', color: "#ee0a24", action: 'delete' }
    ],
  },
  lifetimes: {
    attached() {
      this.getProjectList()
    }
  },
  methods: {
    getProjectList() {
      app.axios('item', {
        op: 'ajaxItemList'
      }).then(res => {
        if (res.code && res.lists && res.lists.length > 0) {
          res.lists.forEach(item => {
            item.showTime = app.dateFormatter(item.uptime)
          })
          this.setData({
            projectList: res.lists
          })
        }
      })
    },
    onAddProject() {
      // 跳转到添加客户页面
      app.navigateTo('/pages/operation-center/add-project/index');
    },
    onCall(e) {
      // 拨打电话逻辑
    },
    onProjectTap(e) {
      this.setData({
        id: e.currentTarget.dataset.id
      })
      app.navigateTo(`/pages/operation-center/project-detail/index?id=${e.currentTarget.dataset.id}`)
      return false
    },
    onProjectPress(e) {
      this.setData({
        showAction: true,
        id: e.currentTarget.dataset.id
      })
      return false
    },
    closeSelect() {
      this.setData({
        showAction: false,
      })
    },
    onSelect(e) {
      const id = this.data.id;
      const alert = this.selectComponent('#v-alert')
      if (e.detail.action == 'view') {
        app.navigateTo(`/pages/operation-center/project-detail/index?id=${id}`)
      } else if (e.detail.action == 'edit') {
        app.navigateTo(`/pages/operation-center/add-project/index?id=${id}`)
      } else if (e.detail.action == 'delete') {
        alert.toggleAlert()
      }
      this.closeSelect()
    },
    handleDelete() {
      const id = this.data.id;
      console.log(id)
      app.axios('item', {
        op: 'ajaxItemDel',
        id: id
      }).then(res => {
        if (res.code) {
          wx.showToast({ title: '删除合同成功', icon: 'success' });
          this.getProjectList()
        }
      })
    }
  }
})